package com.urms.service.serviceImpl;

import com.urms.mapper.UserMapper;
import com.urms.pojo.Result;
import com.urms.pojo.User;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class StudentRole implements UserRole {

    @Resource
    private UserMapper userMapper;

    public static StudentRole studentRoleMapper;

    @PostConstruct
    public void init(){
        studentRoleMapper = this ;
        studentRoleMapper.userMapper = userMapper;
    }
    @Override
    public Result register(User user) {
        if(user.getPhone()==null){
            return Result.error("手机号不能为空");
        }
        if(studentRoleMapper.userMapper.queryStudentByPhone(user.getPhone())!=null){
            return Result.error("该手机号已被注册");
        }
        studentRoleMapper.userMapper.insertStudent(user);
        return Result.success("注册成功");
    }

    @Override
    public Result login(User user) {
        User student = studentRoleMapper.userMapper.queryStudentByPhone(user.getPhone());
        log.info("student:{},user:{}",student,user);
        if(student==null){
            return Result.error("该手机号未注册");
        }
        if(!student.getPassword().equals(user.getPassword())){
            return Result.error("密码错误");
        }
        return Result.success("登录成功",student);
    }
}
